1. basic-sample-network

basic-sample-network

This is the “Hello World” of Hyperledger Composer samples, which demonstrates the core functionality of Hyperledger Composer by changing the value of an asset.

Participant SampleParticipant

Asset SampleAsset

Transaction SampleTransaction

Event SampleEvent

basic

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
it('Alice can read all of the assets', () => {

// Use the identity for Alice.
return useIdentity(aliceCardName)
.then(() => {

// Get the assets.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.getAll();

});

})
.then((assets) => {

// Validate the assets.
assets.should.have.lengthOf(2);
const asset1 = assets[0];
asset1.owner.getFullyQualifiedIdentifier().should.equal('org.acme.sample.SampleParticipant#alice@email.com');
asset1.value.should.equal('10');
const asset2 = assets[1];
asset2.owner.getFullyQualifiedIdentifier().should.equal('org.acme.sample.SampleParticipant#bob@email.com');
asset2.value.should.equal('20');

});

});

Alice can read all of the assets

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
it('Bob can read all of the assets', () => {

// Use the identity for Bob.
return useIdentity(bobCardName)
.then(() => {

// Get the assets.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.getAll();

});

})
.then((assets) => {

// Validate the assets.
assets.should.have.lengthOf(2);
const asset1 = assets[0];
asset1.owner.getFullyQualifiedIdentifier().should.equal('org.acme.sample.SampleParticipant#alice@email.com');
asset1.value.should.equal('10');
const asset2 = assets[1];
asset2.owner.getFullyQualifiedIdentifier().should.equal('org.acme.sample.SampleParticipant#bob@email.com');
asset2.value.should.equal('20');

});

});

bob read

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
it('Alice can add assets that she owns', () => {

// Use the identity for Alice.
return useIdentity(aliceCardName)
.then(() => {

// Create the asset.
const asset3 = factory.newResource('org.acme.sample', 'SampleAsset', '3');
asset3.owner = factory.newRelationship('org.acme.sample', 'SampleParticipant', 'alice@email.com');
asset3.value = '30';

// Add the asset, then get the asset.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.add(asset3)
.then(() => {
return assetRegistry.get('3');
});
});

})
.then((asset3) => {

// Validate the asset.
asset3.owner.getFullyQualifiedIdentifier().should.equal('org.acme.sample.SampleParticipant#alice@email.com');
asset3.value.should.equal('30');

});

});

alice assets

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
it('Alice cannot add assets that Bob owns', () => {

// Use the identity for Alice.
return useIdentity(aliceCardName)
.then(() => {

// Create the asset.
const asset3 = factory.newResource('org.acme.sample', 'SampleAsset', '3');
asset3.owner = factory.newRelationship('org.acme.sample', 'SampleParticipant', 'bob@email.com');
asset3.value = '30';

// Add the asset, then get the asset.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.add(asset3);
});

})
.should.be.rejectedWith(/does not have .* access to resource/);

});

Alice cannot add assets

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
it('Bob can add assets that he owns', () => {

// Use the identity for Bob.
return useIdentity(bobCardName)
.then(() => {

// Create the asset.
const asset4 = factory.newResource('org.acme.sample', 'SampleAsset', '4');
asset4.owner = factory.newRelationship('org.acme.sample', 'SampleParticipant', 'bob@email.com');
asset4.value = '40';

// Add the asset, then get the asset.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.add(asset4)
.then(() => {
return assetRegistry.get('4');
});
});

})
.then((asset4) => {

// Validate the asset.
asset4.owner.getFullyQualifiedIdentifier().should.equal('org.acme.sample.SampleParticipant#bob@email.com');
asset4.value.should.equal('40');

});

});

Bob can add assets that he owns

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
it('Bob cannot add assets that Alice owns', () => {

// Use the identity for Bob.
return useIdentity(bobCardName)
.then(() => {

// Create the asset.
const asset4 = factory.newResource('org.acme.sample', 'SampleAsset', '4');
asset4.owner = factory.newRelationship('org.acme.sample', 'SampleParticipant', 'alice@email.com');
asset4.value = '40';

// Add the asset, then get the asset.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.add(asset4);
});

})
.should.be.rejectedWith(/does not have .* access to resource/);

});

Bob cannot add assets

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
it('Alice can update her assets', () => {

// Use the identity for Alice.
return useIdentity(aliceCardName)
.then(() => {

// Create the asset.
const asset1 = factory.newResource('org.acme.sample', 'SampleAsset', '1');
asset1.owner = factory.newRelationship('org.acme.sample', 'SampleParticipant', 'alice@email.com');
asset1.value = '50';

// Update the asset, then get the asset.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.update(asset1)
.then(() => {
return assetRegistry.get('1');
});
});

})
.then((asset1) => {

// Validate the asset.
asset1.owner.getFullyQualifiedIdentifier().should.equal('org.acme.sample.SampleParticipant#alice@email.com');
asset1.value.should.equal('50');

});

});

Alice can update her assets

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
it('Alice cannot update Bob\'s assets', () => {

// Use the identity for Alice.
return useIdentity(aliceCardName)
.then(() => {

// Create the asset.
const asset2 = factory.newResource('org.acme.sample', 'SampleAsset', '2');
asset2.owner = factory.newRelationship('org.acme.sample', 'SampleParticipant', 'bob@email.com');
asset2.value = '50';

// Update the asset, then get the asset.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.update(asset2);
});

})
.should.be.rejectedWith(/does not have .* access to resource/);

});

Alice cannot update Bob

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
it('Bob can update his assets', () => {

// Use the identity for Bob.
return useIdentity(bobCardName)
.then(() => {

// Create the asset.
const asset2 = factory.newResource('org.acme.sample', 'SampleAsset', '2');
asset2.owner = factory.newRelationship('org.acme.sample', 'SampleParticipant', 'bob@email.com');
asset2.value = '60';

// Update the asset, then get the asset.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.update(asset2)
.then(() => {
return assetRegistry.get('2');
});
});

})
.then((asset2) => {

// Validate the asset.
asset2.owner.getFullyQualifiedIdentifier().should.equal('org.acme.sample.SampleParticipant#bob@email.com');
asset2.value.should.equal('60');

});

});

bob can update his assets

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
it('Bob cannot update Alice\'s assets', () => {

// Use the identity for Bob.
return useIdentity(bobCardName)
.then(() => {

// Create the asset.
const asset1 = factory.newResource('org.acme.sample', 'SampleAsset', '1');
asset1.owner = factory.newRelationship('org.acme.sample', 'SampleParticipant', 'alice@email.com');
asset1.value = '60';

// Update the asset, then get the asset.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.update(asset1);
});

})
.should.be.rejectedWith(/does not have .* access to resource/);

});

Bob cannot update Alice

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
it('Alice can remove her assets', () => {

// Use the identity for Alice.
return useIdentity(aliceCardName)
.then(() => {

// Remove the asset, then test the asset exists.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.remove('1')
.then(() => {
return assetRegistry.exists('1');
});
});

})
.should.eventually.be.false;

});

Alice can remove her assets

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
it('Alice cannot remove Bob\'s assets', () => {

// Use the identity for Alice.
return useIdentity(aliceCardName)
.then(() => {

// Remove the asset, then test the asset exists.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.remove('2');
});

})
.should.be.rejectedWith(/does not have .* access to resource/);

});

Alice cannot remove Bob's assets

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
it('Bob can remove his assets', () => {

// Use the identity for Bob.
return useIdentity(bobCardName)
.then(() => {

// Remove the asset, then test the asset exists.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.remove('2')
.then(() => {
return assetRegistry.exists('2');
});
});

})
.should.eventually.be.false;

});

Bob can remove his assets

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
it('Bob cannot remove Alice\'s assets', () => {

// Use the identity for Bob.
return useIdentity(bobCardName)
.then(() => {

// Remove the asset, then test the asset exists.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.remove('1');
});

})
.should.be.rejectedWith(/does not have .* access to resource/);

});

Bob cannot remove Alice's assets

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
it('Alice can submit a transaction for her assets', () => {

// Use the identity for Alice.
return useIdentity(aliceCardName)
.then(() => {

// Submit the transaction.
const transaction = factory.newTransaction('org.acme.sample', 'SampleTransaction');
transaction.asset = factory.newRelationship('org.acme.sample', 'SampleAsset', '1');
transaction.newValue = '50';
return businessNetworkConnection.submitTransaction(transaction);

})
.then(() => {

// Get the asset.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.get('1');
});

})
.then((asset1) => {

// Validate the asset.
asset1.owner.getFullyQualifiedIdentifier().should.equal('org.acme.sample.SampleParticipant#alice@email.com');
asset1.value.should.equal('50');

// Validate the events.
events.should.have.lengthOf(1);
const event = events[0];
event.eventId.should.be.a('string');
event.timestamp.should.be.an.instanceOf(Date);
event.asset.getFullyQualifiedIdentifier().should.equal('org.acme.sample.SampleAsset#1');
event.oldValue.should.equal('10');
event.newValue.should.equal('50');

});

});

Alice can submit a transaction for her assets

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
it('Alice cannot submit a transaction for Bob\'s assets', () => {

// Use the identity for Alice.
return useIdentity(aliceCardName)
.then(() => {

// Submit the transaction.
const transaction = factory.newTransaction('org.acme.sample', 'SampleTransaction');
transaction.asset = factory.newRelationship('org.acme.sample', 'SampleAsset', '2');
transaction.newValue = '50';
return businessNetworkConnection.submitTransaction(transaction);

})
.should.be.rejectedWith(/does not have .* access to resource/);

});

Alice cannot submit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
it('Bob can submit a transaction for his assets', () => {

// Use the identity for Bob.
return useIdentity(bobCardName)
.then(() => {

// Submit the transaction.
const transaction = factory.newTransaction('org.acme.sample', 'SampleTransaction');
transaction.asset = factory.newRelationship('org.acme.sample', 'SampleAsset', '2');
transaction.newValue = '60';
return businessNetworkConnection.submitTransaction(transaction);

})
.then(() => {

// Get the asset.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.get('2');
});

})
.then((asset2) => {

// Validate the asset.
asset2.owner.getFullyQualifiedIdentifier().should.equal('org.acme.sample.SampleParticipant#bob@email.com');
asset2.value.should.equal('60');

// Validate the events.
events.should.have.lengthOf(1);
const event = events[0];
event.eventId.should.be.a('string');
event.timestamp.should.be.an.instanceOf(Date);
event.asset.getFullyQualifiedIdentifier().should.equal('org.acme.sample.SampleAsset#2');
event.oldValue.should.equal('20');
event.newValue.should.equal('60');

});

});

Bob can submit a transaction for his assets

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
it('Bob cannot submit a transaction for Alice\'s assets', () => {

// Use the identity for Bob.
return useIdentity(bobCardName)
.then(() => {

// Submit the transaction.
const transaction = factory.newTransaction('org.acme.sample', 'SampleTransaction');
transaction.asset = factory.newRelationship('org.acme.sample', 'SampleAsset', '1');
transaction.newValue = '60';
return businessNetworkConnection.submitTransaction(transaction);

})
.should.be.rejectedWith(/does not have .* access to resource/);

});

Bob cannot submit a transaction for Alice